﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataTables;
using workflowMVC.Models;
using DataTool;
using OneGateWF;
using Common;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;

namespace workflowMVC.Controllers
{
    [Authorize(Roles="Student")]
    public class TaskController : Controller
    {
        //
        // GET: /Task/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ConfirmStudent()
        {            
            ViewBag.Title = "Xác nhận sinh viên";
            ViewBag.TaskName = "Xác nhận sinh viên";
            ViewBag.Message = "Bạn có muốn làm đơn xác nhận sinh viên hay không ?";
            ViewBag.FormSource = Url.Action("ConfirmStuForm");
            return View("ConfirmSimpleTask");
        }
        [HttpPost]
        public ActionResult ConfirmStudent(SimpleTaskModel m)
        {
            ValidateTaskHost host = new ValidateTaskHost();
            host.InvokeValidateTaskWF(new SimpleTask
            {
                NumCode = User.Identity.Name,
                TaskKind = 1,
                Status = 1,
                Reason = m.Reason
            }, new CreateAdditionalInfo { Note=m.Note});
            ViewBag.NotifyResult = "Bạn đã yêu cầu làm 1 giấy xác nhận sinh viên thành công. Hãy chờ đợi cho đến khi được kiểm duyệt";
            return View("NotifyResult");
        }


        [HttpPut]
        public ActionResult ConfirmStuForm(SimpleTaskModel m)
        {
            wfdbEntities db = new wfdbEntities();
            var Student = db.STUDENTs.Where(x => x.NumCode == User.Identity.Name).SingleOrDefault();
            this.HttpContext.Session["Report_Kind"] = "ConfirmStudentForm";
            this.HttpContext.Session["Report_Name"] = Student.Name;
            this.HttpContext.Session["Report_NumCode"] = User.Identity.Name;
            this.HttpContext.Session["Report_Birthday"] = Student.DoB;
            this.HttpContext.Session["Report_Address"] = Student.Address;
            this.HttpContext.Session["Report_Class"] = Student.Class;
            this.HttpContext.Session["Report_StartYear"] = Student.StartYear;
            this.HttpContext.Session["Report_TimeLast"] = Student.TIMELAST1.Description;
            this.HttpContext.Session["Report_EduMode"] = Student.EDUMODE1.Description;
            this.HttpContext.Session["Report_SYear"] = DateTime.Now.Year - Student.StartYear;
            this.HttpContext.Session["Report_Reason"] = m.Reason;
            this.HttpContext.Session["Report_Department"] = Student.CLASS1.DEPARTMENT1.Description;
            return JavaScript("window.open('" + Url.Action("DocForm.aspx", "AspNetForms") + "');");
        }


        public ActionResult ExcemptTuition()
        {
            ViewBag.FormSource = Url.Action("ExcemptTuitionForm");
            return View();
        }
        [HttpPost]
        public ActionResult ExcemptTuition(ExcemptTuitionTaskStuModel m)
        {
            ValidateTaskHost host = new ValidateTaskHost();
            host.InvokeValidateTaskWF(new ExcemptTuitionTask
            {
                NumCode = User.Identity.Name,
                TaskKind = 2,
                Status = 1,
                District = m.District,
                Hometown = m.Hometown,
                ParentName = m.ParentName,
                Province = m.Province,
                Street = m.Street,
                Type = m.Type,
                Ward = m.Ward,
            }, new CreateAdditionalInfo { Note=m.Note});
            ViewBag.NotifyResult = "Bạn đã yêu cầu làm đơn xin miễn giảm học phí thành công. Hãy chờ đợi cho đến khi được kiểm duyệt";
            return View("NotifyResult");
        }

        [HttpPut]
        public ActionResult ExcemptTuitionForm(ExcemptTuitionTaskStuModel m)
        {
            wfdbEntities db = new wfdbEntities();
            var Student = db.STUDENTs.Where(x => x.NumCode == User.Identity.Name).SingleOrDefault();
            this.HttpContext.Session["Report_Kind"] = "ExcemptTuitionForm";
            this.HttpContext.Session["Report_Name"] = Student.Name;
            this.HttpContext.Session["Report_NumCode"] = Student.NumCode;
            this.HttpContext.Session["Report_Department"] = Student.CLASS1.DEPARTMENT1.Description;
            this.HttpContext.Session["Report_Birthday"] = Student.DoB;
            this.HttpContext.Session["Report_Hometown"] = m.Hometown;
            this.HttpContext.Session["Report_ParentName"] = m.ParentName;
            this.HttpContext.Session["Report_Street"] = m.Street;
            this.HttpContext.Session["Report_Ward"] = m.Ward;
            this.HttpContext.Session["Report_District"] = m.District;
            this.HttpContext.Session["Report_Province"] = m.Province;
            this.HttpContext.Session["Report_Type"] = m.Type;
            this.HttpContext.Session["Report_Class"] = Student.Class;
            this.HttpContext.Session["Report_SYear"] = Util.getSYear(Student.StartYear.Value);
            this.HttpContext.Session["Report_StartYear"] = Student.StartYear;
            this.HttpContext.Session["Report_TimeLast"] = Student.TIMELAST1.Description;

            return JavaScript("window.open('" + Url.Action("DocForm.aspx", "AspNetForms") + "');");
        }

        public ActionResult StudentCard()
        {
            ViewBag.Title = "Cấp thẻ sinh viên";
            ViewBag.TaskName = "Cấp thẻ sinh viên";
            ViewBag.Message = "Bạn có muốn làm đơn xin cấp thẻ sinh viên hay không ?";
            ViewBag.FormSource = Url.Action("StudentCardForm");
            return View("ConfirmSimpleTask");
        }

        [HttpPost]
        public ActionResult StudentCard(SimpleTaskModel m)
        {
            ValidateTaskHost host = new ValidateTaskHost();
            host.InvokeValidateTaskWF(new SimpleTask
            {
                NumCode = User.Identity.Name,
                TaskKind = 3,
                Status = 1,
                Reason = m.Reason,
            }, new CreateAdditionalInfo { Note = m.Note});
            ViewBag.NotifyResult = "Bạn đã yêu cầu làm 1 giấy xin cấp thẻ sinh viên thành công. Hãy chờ đợi cho đến khi được kiểm duyệt";
            return View("NotifyResult");
        }

        [HttpPut]
        public ActionResult StudentCardForm(SimpleTaskModel m)
        {
            wfdbEntities db = new wfdbEntities();
            var Student = db.STUDENTs.Where(x => x.NumCode == User.Identity.Name).SingleOrDefault();
            this.HttpContext.Session["Report_Kind"] = "StudentCardForm";
            this.HttpContext.Session["Report_Name"] = Student.Name;
            this.HttpContext.Session["Report_NumCode"] = User.Identity.Name;
            this.HttpContext.Session["Report_Birthday"] = Student.DoB.Value.ToShortDateString();
            this.HttpContext.Session["Report_EduMode"] = Student.EDUMODE1.Description;
            this.HttpContext.Session["Report_SYear"] = DateTime.Now.Year - Student.StartYear;
            this.HttpContext.Session["Report_Reason"] = m.Reason;
            this.HttpContext.Session["Report_Department"] = Student.CLASS1.DEPARTMENT1.Description;
            return JavaScript("window.open('" + Url.Action("DocForm.aspx", "AspNetForms") + "');");
        }

        public ActionResult ProcessingList()
        {
            return View();
        }

        public JsonResult ProcessingListData()
        {
            var db = new wfdbEntities();
            var data = db.TASKs.AsEnumerable().Where(x => x.NumCode == User.Identity.Name && x.Status <=3).Select(x => new TrackTaskListStudentModel()
                {
                    date = x.LOGs.FirstOrDefault(z => z.Kind == 1).Date.Value,
                    id = x.Id,
                    status = x.TASKSTATU.Description,
                    taskkind = x.TASKKIND.Description
                });
            if (Request["sEcho"] != null)
            {
                var parser = new DataTableParser<TrackTaskListStudentModel>(Request, data.AsQueryable());
                var cs = parser.Parse();
                cs.aaData.ForEach(
                x =>
                    {
                        string idx = x[3];
                        x[3]="";
                        if (x[2] == "Khởi tạo")
                            x[3] = Util.getsTableLink(new { @class = Util.delete }, new { title = "Hủy bỏ", @class = "delete-button", href = Url.Action("Cancel", new { id = idx }) });
                        x[3] += Util.getsTableLink(new { @class = Util.detail }, new { title = "Xem đơn", href = Url.Action("getForm","TaskForm", new { id = idx }) });
                        
                    });
                return Json(cs);
            }
            return Json(data);
        }

        public string Cancel(Guid id)
        {
            wfdbEntities db = new wfdbEntities();
            var t = db.TASKs.FirstOrDefault(x=> x.Id == id && x.Status == 1);
            if (t == null)
                return "Không thể xóa bài";
            ValidateTaskHost h = new ValidateTaskHost();
            h.Cancel(id);
            return "";
        }
    }
}
